package com.loyal.loyalerp.basedata.permission.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.basedata.action.mapper.GyActionMapper;
import com.loyal.loyalerp.basedata.action.model.GyAction;
import com.loyal.loyalerp.basedata.permission.mapper.GyPermissionMapper;
import com.loyal.loyalerp.basedata.permission.model.GyPermission;
import com.loyal.loyalerp.basedata.permission.model.vo.GridVO;
import com.loyal.loyalerp.basedata.permission.service.PermissionService;
import com.loyal.loyalerp.commons.ServerResponse;
import com.loyal.loyalerp.system.mapper.GySystemMapper;
import com.loyal.loyalerp.system.model.GySystem;
import com.loyal.loyalerp.utils.ValidateUtil;


@Controller
@RequestMapping("/permission")
public class PermissionController{
	
	final static String permission = "gy_permission";
	
	@Autowired
	PermissionService permissionService;
	
	@Autowired
	GyActionMapper actionMapper;
	
	@Autowired
	GySystemMapper systemMapper;
	
	@Autowired
	GyPermissionMapper permissionMapper;
	
	@RequestMapping("/jsp")
	public String catttt() {
		return "basedata/permission/permission";
	}

	@RequestMapping("/editPermission")
	public String editPermission(HttpServletRequest request) {
		List<GyAction> actionList=actionMapper.listGyAction();
		List<GySystem> systemList=systemMapper.selectAll(null);
		List<GyPermission> permissionList=permissionMapper.listPermission();
		request.getSession().setAttribute("actionList", actionList);
		request.getSession().setAttribute("systemList", systemList);
		request.getSession().setAttribute("permissionList", permissionList);
		
		return "basedata/permission/editPermission";
	}
	
	@RequestMapping("/searchPermission")
	public String searchPermission() {
		return "basedata/permission/searchPermission";
	}
	
	@ResponseBody
	@RequestMapping("/edit") // 增删改
	public Object editUser(String systemcode,String oper, @Valid GyPermission permission,BindingResult result,HttpServletResponse response) throws IOException {
		
		if(result.hasErrors()){
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			return ServerResponse.createByError("参数验证失败",result.getAllErrors());
		}
		/*
		Boolean bool = permissionService.editPermission(oper, permission);
		if (bool) {
			return ServerResponse.createBySuccess();
		}
		return ServerResponse.createByError();
		*/
		System.out.println(permission);
		
		int status = permissionService.editPermission(oper, permission);
		if (status == 200) {
			return ServerResponse.createBySuccess();
		}else if(status == 888){
			return ServerResponse.createByError("有外键约束！");
		}else if(status == 600){
			return ServerResponse.createByError("没有权限！");
		}else{
			return ServerResponse.createByError();
		}
		
	}
	

	
	
	@ResponseBody
	@RequestMapping("/listPermission")
	public Object listUser(HttpServletRequest request, String _search, Integer rows, Integer page, String sidx,String sord,String searchField, String searchString) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		
		if(!ValidateUtil.validateQuery(permission)) {
			return ServerResponse.createByError("没有权限");
		}
		
		
		if ("false".equals(_search) || StringUtils.isEmpty(_search))// 列表显示
		{
			PageInfo<GyPermission> pageInfo = permissionService.listPermission(page, rows, sidx, sord);
			
			return GridVO.fromPageInfo(pageInfo);
		} 
		//模糊查询
		else {
			PageInfo<GyPermission> pageInfo = permissionService.findPermission(page, rows, sord, searchField, searchString);
			return GridVO.fromPageInfo(pageInfo);
		}

	}
	
}
